package vn.personal.anhht.bloback12.activity;

import vn.personal.anhht.bloback12.util.Constants;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class SplashScreen extends BloBack12Activity {

	private ProgressBar mProgress;
    private int mProgressStatus;
    private boolean isRunning;
    private String TAG = "SplashScreen";
    Handler handler = new Handler(){
    	@Override
    	public void handleMessage(Message msg){
    		mProgressStatus = mProgressStatus + Constants.PROGRESS_INC;
    		mProgress.incrementProgressBy(Constants.PROGRESS_INC);
    		if(mProgressStatus >= 100){
    			finish();
    			startMyActivity(HomeActivity.class);
//    			Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
//    			startActivity(intent);
    		}
    	}
    };

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splashscreen);
		checkLogin();
	}
	
	private void showProgress(){
	    mProgressStatus = 0;
        mProgress = (ProgressBar) findViewById(R.id.pgbSplashScreen);

        mProgress.setProgress(0);
        isRunning = true;
        //create a thread for updating progress bar
        Thread background = new Thread(new Runnable() {
            
            public void run() {
                try {
                    for (int i = 0; i < 10 && isRunning; i++) {
                        // wait 250ms between each update
                        Thread.sleep(Constants.PROGRESS_SLEEP);
                        handler.sendMessage(handler.obtainMessage());                   
                    }
                } catch (Exception e) {
                    logE(TAG, e.toString());
                }
            }
        });
        background.start();
	}
	
	@Override
	public void onStop(){
		super.onStop();
		isRunning = false;
	}
	
	public void checkLogin() {
        if (getSharedPreferences(
                Constants.SETTINGS_SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getBoolean(
                        getResources().getString(R.string.key_pref_turn_pass), false)) {
            startActivityForResult(new Intent(getApplicationContext(),
                    LoginActivity.class), Constants.REQUEST_LOGIN);
        } else {
            showProgress();
        }
    }
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case Constants.REQUEST_LOGIN: {
            if (Constants.RESPONSE_LOGIN_OK == resultCode) {
                showProgress();
                // initMain();
            } else if (Constants.RESPONSE_LOGIN_CANCEL == resultCode) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }

        }
        }
    }
}
